package com.dunqi.gpm.shunqing.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.view.ComTitleBar;
import com.dunqi.gpm.shunqing.home.bean.Project;
import com.dunqi.gpm.shunqing.home.eventbus.FinishAndRefresh;
import com.dunqi.gpm.shunqing.home.fragment.ProjectTimelineFragment;
import com.dunqi.gpm.shunqing.home.widget.ProjectPhoneAndDes;
import com.dunqi.gpm.shunqing.home.widget.ProjectTimeLineHeader;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;

/**
 * 用fragment  这个不做使用
 */
public class ProjectTimelineActivity extends BaseAct {

    private static final String PROJECT = "project";

    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.projectPhoneAndDes)
    ProjectPhoneAndDes projectPhoneAndDes;
    @BindView(R.id.up_tv)
    TextView upTv;
    @BindView(R.id.project_time_line_header)
    ProjectTimeLineHeader projectTimeLineHeader;
    private Project project;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {

    }

    @Override
    protected void initUiAndListener() {
        EventBus.getDefault().register(this);
        Intent intent = getIntent();
        if (intent != null) {
            project = (Project) intent.getSerializableExtra(PROJECT);
        }
        titleBar.setTitle(project.getProjectName());

        projectPhoneAndDes.setData(project);

        ProjectTimelineFragment fragment = ProjectTimelineFragment.newInstance(project);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, fragment)
                .commit();

        projectTimeLineHeader.getGridView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ProjectDetailActivity.start(getActivity(), project, position);
            }
        });

        upTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProjectDetailActivity.start(getActivity(), project, 8);
            }
        });

        titleBar.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe
    public void onEventMainThread(FinishAndRefresh finishAndRefresh) {
        finish();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_timeline;
    }

    public static void start(Context context, Project project) {
        Intent intent = new Intent(context, ProjectTimelineActivity.class);
        intent.putExtra(PROJECT, project);
        context.startActivity(intent);
    }
}
